//
//  lessonViewController.m
//  private_kitchen
//
//  Created by lirui on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "LessonViewController.h"


@interface LessonViewController ()<UITableViewDataSource,UITableViewDelegate,LunBoTuDelegate>
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic ,retain)NSMutableArray *lunArr;
@property(nonatomic, retain)UIView *aView;
@property(nonatomic, assign)BOOL isRefrsh;
@end

static NSInteger i = 0;
@implementation LessonViewController
- (void)dealloc
{
    [_dataArr release];
    [_tableview release];
    [_lunArr release];
    [_aView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self AFNpost];
    [self post3];
    [self creatTableview];
    [self loadMBProgress];
    self.aView =[[ UIView alloc ] initWithFrame:CGRectMake(0, 0, 375*WIDTH, 155 *HEIGHT)];
    _aView.backgroundColor = [UIColor grayColor];
    _aView.alpha = 0.8;
    self.tableview.tableHeaderView = self.aView;
    [_aView release];
    UILabel *label = [[ UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375,HEIGHT* 50)];
    label.text = @"已显示全部";
    label.textAlignment = NSTextAlignmentCenter;
    self.tableview.tableFooterView = label;
    
    self.navigationItem.title = @"学做菜";

    
}
//tableview数据请求
-(void)AFNpost
{
    NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=CourseIndex&page=%ld&size=10&token=0&user_id=0&version=4.1",i];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (_isRefrsh) {
            
        
        NSArray *arr  = responseObject[@"data"][@"data"];
        
        self.dataArr = [ NSMutableArray array];
        self.dataArr = [ LessonModel modelHanderWithArray:arr];
       
            [_tableview reloadData];
            [self.hud removeFromSuperview];
            [self.tableview.mj_header endRefreshing];
    }
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
                       }];
    
}
//轮播图数据请求
-(void)post3
{
    NSString *str = @"http://api.izhangchu.com/?methodName=CourseIndex&page=1&size=10&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr  = responseObject[@"data"][@"top"];
        NSLog(@"%@",arr);
        self.lunArr = [ NSMutableArray array];
        self.lunArr = [ LessonModel modelHanderWithArray:arr];
        NSMutableArray *temp =[ NSMutableArray array];
        for (NSInteger i = 0; i < _lunArr.count; i++) {
            LessonModel *m =  [self.lunArr objectAtIndex:i];
            [temp addObject:m.banner_picture];
        }
        LunBoTu *lun = [[ LunBoTu alloc] init];
        lun.delegate = self;
        lun.time = 2;
        [lun imageWithUrlArray:temp frame:CGRectMake(0, 0, 375 *WIDTH, 150 *HEIGHT)];
        [self.aView addSubview:[lun collectionView]];
        [self.aView addSubview:[lun pageControl]];
        
        
    }
     
     
     
    failure:^(NSURLSessionDataTask *task, NSError *error) {
                       }];
    
}
//创建tableview
-(void)creatTableview
{
    
    self.tableview =[[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667 - 49*HEIGHT) style:UITableViewStylePlain];
    self.tableview.backgroundColor = [UIColor whiteColor];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    [self.view addSubview:_tableview];
    [_tableview registerClass:[LessonTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableview release];
    //上拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefrsh = YES;
        
        [self  AFNpost];
    }];
    self.tableview.mj_header = header;
    [self.tableview.mj_header beginRefreshing];

    
}
#pragma mark -必须实现的协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   LessonTableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    LessonModel *m = _dataArr[indexPath.row];
     [cell.img sd_setImageWithURL:[NSURL URLWithString:m.image]];
    //cell.title.text = m. series_name;
    //切分数组
     NSArray *arr = [ m.series_name componentsSeparatedByString:@"#"];
     cell.title.text = arr[2];
    cell.title.textColor = [UIColor whiteColor];
    cell.sum.text = [NSString stringWithFormat:@"%@  %@/%@集   %@人做过",arr[1],m.episode,m.episode_sum,m.play];
    cell.sum.textColor =[ UIColor whiteColor];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 *HEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LessondetailViewController *detail =[[ LessondetailViewController alloc] init];
    detail.model = _dataArr[indexPath.item];
    [self.navigationController pushViewController:detail  animated:YES];
}
-(void)didSelectCollectionView:(UICollectionView *)collectionView atItemImdexPath:(NSIndexPath *)indexPath
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
